package com.ruangong.GitClient.Test.YanZhaoWangTest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ruangong.GitClient.YanZhaoWang.YanZhaoWang;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询所有研究学科代码测试
 */

public class Test5 {
    public static void main(String[] args) throws IOException {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
//        String url = "https://yz.chsi.com.cn/zsml/zyfx_search.jsp";
        File file = new File("app/src/main/java/com/ruangong/GitClient/Test/YanZhaoWangTest/yzw_select.html");
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Document doc = Jsoup.parse(file, "UTF-8", "");
        // 查找所有的 <select> 标签
        Elements selects = doc.select("select");
        Map<String, String> ResearchDisciplineMap = new LinkedHashMap<>();
        // 遍历每一个 <select>
        for (Element select : selects) {
            System.out.println("Select ID: " + select.id());
            // 在当前 <select> 中查找所有的 <option> 标签
            Elements options = select.select("option");
            // 遍历每一个 <option>
            for (Element option : options) {
                // 提取每个 <option> 的 value 属性和文本
                String value = option.attr("value");
                String text = option.text();
                ResearchDisciplineMap.put(value, text);
                System.out.println("Option value: " + value + ", Text: " + text);
            }
            System.out.println(); // 添加空行以便区分不同的 <select> 输出
        }
        String json = gson.toJson(ResearchDisciplineMap);
        try (Writer writer = new FileWriter("app/src/main/java/com/ruangong/GitClient/YanZhaoWang/Research_Discipline_Code.json")) {
            writer.write(json);
        }
        System.out.println("JSON file was created successfully.");
        System.out.println(YanZhaoWang.getResearchDisciplineLocally("0812"));
        System.out.println(YanZhaoWang.getResearchDisciplineAllCodeLocally());
    }
}
